#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 210;
int dis[N][N];
char g[N][N];
int n, m;
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };
void bfs(int x, int y)
{
	dis[x][y] = 0;
	queue<PII>q;
	q.push({ x,y });
	while (q.size()) {
		PII t = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int a = dx[i] + t.x;
			int b = dy[i] + t.y;
			if (g[a][b] == '#' || dis[a][b] != -1) {
				continue;
			}
			if (a <= 0 || a > n || b <= 0 || b > m) {
				continue;
			}
			dis[a][b] = dis[t.x][t.y] + 1;
			q.push({ a,b });
		}
	}
}
int main()
{
	cin >> n >> m;
	int x = 0, y = 0;
	memset(dis, -1, sizeof(dis));
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> g[i][j];
			if (g[i][j] == 'S') {
				x = i, y = j;
			}
		}
	}
	bfs(x, y);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cout << dis[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}